import { EventType } from "./../Base/EventType";
import GameInstance from "./../GameInstance";

const { ccclass, property } = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
    @property(cc.Node)
    public bgLayer: cc.Node = null;



    @property(cc.Node)
    public renderCamera: cc.Node = null;

    onLoad() {
        this.node.on(cc.Node.EventType.MOUSE_WHEEL, (event) => {
            // console.log("event", event);
            let scrollY = event.getScrollY();
            let localPos = event.getLocation();
            localPos = this.node.convertToNodeSpaceAR(localPos);
            this.scaleRenderLayer(scrollY, localPos);
        });
        let touchStartPos = cc.v2(0, 0);
        this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
            touchStartPos = event.getLocation();
            touchStartPos = this.node.convertToNodeSpaceAR(touchStartPos);
        });
        this.node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
            let localPos = event.getLocation();
            localPos = this.node.convertToNodeSpaceAR(localPos);
            // this.renderCamera.position÷
            let disPos = touchStartPos.sub(localPos);
            this.renderCamera.position = this.renderCamera.position.add(cc.v3(disPos.x, disPos.y, 0));
            touchStartPos = localPos;
        });
    }
    start() {
        GameInstance.getInstance().createOneNode();
    }
    /**
    * 缩放渲染层
    * @param scale 缩放值
    * @param localPos 缩放锚点
    */
    public scaleRenderLayer(scale: number, localPos: cc.Vec2) {
        let camera = cc.find('Canvas/RenderLayerCamera');
        if (camera && camera.getComponent(cc.Camera)) {
            let cameraCom = camera.getComponent(cc.Camera);
            cameraCom.zoomRatio += 0.02 * scale / Math.abs(scale);
            console.log("camera com ", cameraCom.zoomRatio);
            if (cameraCom.zoomRatio > 1.5){
                cameraCom.zoomRatio = 1.5
            }
            if (cameraCom.zoomRatio < 0.5){
                cameraCom.zoomRatio = 0.5;
            }
        }
    }
}
